package com.xuqi.DouBan;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URL;
import java.util.ArrayList;

import com.xuqi.R;
import com.xuqi.DouBan.DouBanSearchResultItem.LinkInfo;
import com.xuqi.DownLoad.W3DownloadControl;
import com.xuqi.DownLoad.W3DownloadControl.DownloadCallBack;
import com.xuqi.menucontrol.MenuControlIF;
import com.xuqi.menucontrol.NSTriggerID;
import com.xuqi.menucontrol.NSTriggerInfo;
import com.xuqi.R.color;

import android.R.integer;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class DouBanSearchListAdapter extends BaseAdapter
{

	private static final String TAG = "DouBanSearchListAdapter";
	private ArrayList<DouBanSearchResultItem> m_list = null;
	private Context m_cContext = null;

	public DouBanSearchListAdapter(ArrayList<DouBanSearchResultItem> list, Context context)
	{
		super();
		m_list = list;
		m_cContext = context;
		// TODO Auto-generated constructor stub
	}

	@Override
	public int getCount()
	{
		// TODO Auto-generated method stub
		return m_list.size();
	}

	@Override
	public Object getItem(int position)
	{
		// TODO Auto-generated method stub
		return m_list.get(position);
	}

	@Override
	public long getItemId(int position)
	{
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent)
	{
		ViewHolder holder = null;
		LayoutInflater inflater = LayoutInflater.from(m_cContext);

		if (convertView == null)
		{
			convertView = inflater.inflate(com.xuqi.R.layout.result_item, null);
			// set the item
			holder = new ViewHolder();
			holder.f_Title = (TextView) convertView.findViewById(R.id.textView6);
			holder.f_Area = (TextView) convertView.findViewById(R.id.textView1);
			holder.f_Actor = (TextView) convertView.findViewById(R.id.textView2);
			holder.f_Language = (TextView) convertView.findViewById(R.id.textView3);
			holder.f_Director = (TextView) convertView.findViewById(R.id.textView4);
			holder.f_RateNum = (TextView) convertView.findViewById(R.id.textView5);
			holder.f_Image = (ImageView) convertView.findViewById(R.id.imageView1);
			convertView.setTag(holder);
		}

		else
		{
			holder = (ViewHolder) convertView.getTag();
		}
		// TODO Auto-generated method stub
		DouBanSearchResultItem item = m_list.get(position);
		holder.f_Title.setText(item.title);
		holder.f_Actor.setText(item.author.size() > 0 ? item.author.get(0) : "");
		
		for(final LinkInfo info : item.link)
		{
			if(info.rel.equals("image"))
			{
				String path = info.href;
				Log.d(TAG, info.href);
				final String filename = path.substring(path.lastIndexOf("/"));
				Log.d(TAG, path);
				final String strpath = "/sdcard/TestImage/";
				File folder = new File(strpath);
				if(!folder.exists())
				{
					folder.mkdir();
				}
				File file = new File(strpath + filename);
				if(file.exists())
				{
					holder.f_Image.setImageBitmap(BitmapFactory.decodeFile(file.getPath()));
				}
				else
				{
					holder.f_Image.setImageBitmap(null);
					final W3DownloadControl downloadControl = new W3DownloadControl();
					downloadControl.init();
					downloadControl.setCallBack(new DownloadCallBack()
					{
						
						@Override
						public void downloadTotalSize(int totalsize)
						{
							// TODO Auto-generated method stub
							
						}
						
						@Override
						public void downloadStart(String fileName)
						{
							// TODO Auto-generated method stub
							
						}
						
						@Override
						public void downloadProgress(String fileName, int progress)
						{
							// TODO Auto-generated method stub
							
						}
						
						@Override
						public void downloadFinished(String fileName, int result)
						{
							// TODO Auto-generated method stub
							NSTriggerInfo TriggerInfo = new NSTriggerInfo();
							TriggerInfo.SetTriggerID(NSTriggerID.UIC_MN_TRG_DOUBAN_IMAGE_DOWNLOAD);
							MenuControlIF.SendTriggerForScreen(TriggerInfo);
							
						}
					});
					Thread thread = new Thread()
					{

						@Override
						public void run()
						{
							// TODO Auto-generated method stub
							super.run();
							downloadControl.downloadFile(info.href, strpath + filename);
						}
						
					};
					thread.start();
					
				}
				
			}
		}
		return convertView;
	}
	
	class ViewHolder
	{
		public TextView f_Title;
		public TextView f_Area;
		public ImageView f_Image;
		public TextView f_Actor;
		public TextView f_Language;
		public TextView f_Director;
		public TextView f_RateNum;
	}
}
